// pages/userOrder/userOrder.js
let api = require("../../api/api.js")

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusType: ["全部", "待付款", "待评价", "已完成"],
    currentType: 0,
    currentTypeParam: "",
    orderInfoList:[],
    order_id: -1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getAllOrder()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    console.log(this.data.order_id)
    let order_id = this.data.order_id
    if(order_id == -1) return
    let index = -1;
    let orderList = that.data.orderInfoList;
    for(let i = 0; i < that.data.orderInfoList.length; i++){
      if(that.data.orderInfoList[i].id == order_id){
        orderList.splice(index, 1)
        that.setData({ orderInfoList: orderList })
        break
      }
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  statusTap: function (e) {
    var obj = e;
    var count = 0;
    for (var key in obj) {
      count++;
    }
    if (count == 0) {
      var curType = 0;
    } else {
      console.log('出现Cannot read property "dataset" of undefined;这样的错误是正常的，不用管！');
      var curType = e.currentTarget.dataset.index;
    }
    this.setData({
      currentType: curType
    });
    console.log(this.data.currentType)
    this.changeType()
    this.onShow();
  },

    /**
   *  修改statusType 
   */
  changeType:function(){
    let that = this
    switch(that.data.currentType){
      case 0:
        that.getAllOrder()
        return
      case 1:
        that.setData({ currentTypeParam: "PAYING" })
        break;
      case 2:
        that.setData({ currentTypeParam: "TRADE_SUCCESS" })
        break;
      case 3:
        that.setData({ currentTypeParam: "TRADE_FINISHED" })
        break;
    }
    that.getStatusOrder()
  },

  /**
   * 获取指定状态订单
   */
  getStatusOrder:function(){
    var that = this
    api.getStatusOrder(that.data.currentTypeParam).then(res=>{
      console.log(res)
      that.setData({ orderInfoList: res.data })
      wx.hideLoading()
    })
  },

  /**
   * 获取所有订单状态
   */
  getAllOrder:function(){
    let that = this;
    api.getAllOrder().then(res=>{
      console.log(res)
      that.setData({ orderInfoList: res.data })
      wx.hideLoading()
    })
  },

  /**
   * 立即支付
   */
  toPayTap:function(e){
    console.log(e)
    let orderInfo = e.target.dataset.info
    api.startPay(orderInfo).then(res=>{
      console.log(res)
    })
  },

  /**
   * 取消订单
   */
  cancelOrderTap:function(e){
    let that = this
    let order_id = e.target.dataset.info
    console.log(order_id)
    wx.showModal({
      title: '提示',
      content: '是否移除收藏',
      success:function(res){
        if(res.confirm){

          api.deleteOrder(order_id).then(res=>{
            let index = -1;
            let orderList = that.data.orderInfoList;
            for(let i = 0; i < that.data.orderInfoList.length; i++){
              if(that.data.orderInfoList[i].id == order_id){
                orderList.splice(index, 1)
                that.setData({ orderInfoList: orderList })
                break
              }
            }
          })

        }else{
          console.log("cancel")
        }
      }
    })
  
  },

  /**
   * 订单详情
   */
  orderDetail:function(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/orderDetial/orderDetial?order_id='+ e.currentTarget.dataset.id,
    })
  },

  /**
   * 订单评价 
   */
  toEvaluate:function(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/evaluate/evaluate?order_id='+e.target.dataset.info.id,
    })
  }

})